package com.suning.sawp.in.controller.task;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.in.controller.DjBaseController;
import com.suning.sawp.in.controller.MyGsonView;
import com.suning.sawp.intf.mainpushnew.MainPushTaskHqHandleService;
import com.suning.sawp.intf.mainpushnew.MainPushTaskHqViewService;
import com.suning.sawp.po.mainpushnew.HqTask;
import com.suning.sawp.service.util.ExcelUtils;
import com.suning.sawp.vo.mainpushnew.BiAreaTaskProgressVo;

/**
 * 
 * 总部主推任务查看控制层<br>
 *
 * @author 14060886 祝翔龙
 */
@RequestMapping("/adminPage/task/hq/view")
@Controller
public class MainPushTaskHqViewController extends DjBaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);

	@Autowired
	private MainPushTaskHqViewService mainPushTaskHqViewService;
	@Autowired
    private MainPushTaskHqHandleService mainPushTaskHqHandleService;

	/**
     * 
     * 功能描述: 查询总部任务完成进度<br>
     *
     * @param hqTaskId
     * @return
     */
    @RequestMapping("/queryHqTaskProgress.do")
    public MyGsonView queryHqTaskProgress(Long hqTaskId) {
        LOGGER.debug("queryHqTaskProgress input hqTaskId: " + hqTaskId);
        return setGsonView(mainPushTaskHqViewService.queryHqTaskProgress(hqTaskId));
    }
    
    
    /**
     * 
     * 功能描述: 查询大区任务完成进度<br>
     *
     * @param hqTaskId
     * @param pageParams
     * @return
     */
    @RequestMapping("/queryAreaTaskProgress.do")
    public MyGsonView queryAreaTaskProgress(Long hqTaskId, String queryParam, Integer pageNo, Integer pageSize) {
        LOGGER.debug("queryAreaTaskProgress input hqTaskId: " + hqTaskId + ";queryParam：" + queryParam + ";pageNo: " + pageNo + ";pageSize: " + pageSize);
        return setGsonView(mainPushTaskHqViewService.queryAreaTaskProgress(hqTaskId, queryParam, super.getPageParams(pageNo, pageSize)));
    }
    
    /**
     * 
     * 功能描述: 查询款数完成进度<br>
     *
     * @param hqTaskId
     * @param disCode
     * @return
     */
    @RequestMapping("/queryModelProgress.do")
    public MyGsonView queryModelProgress(Long hqTaskId, String disCode) {
        LOGGER.debug("queryModelProgress input hqTaskId: " + hqTaskId + ", disCode=" + disCode);
        return setGsonView(mainPushTaskHqViewService.queryModelProgress(hqTaskId, disCode));
    }
	
	
	 @RequestMapping("/exportTaskProgress.do")
	 public void exportTaskProgress(HttpServletResponse response, Long hqTaskId) throws IOException {
	     OutputStream outputStream = null;
	     Workbook workbook = null;
	     try {
	         List<BiAreaTaskProgressVo> areaTaskProgressInfo = mainPushTaskHqViewService.queryAreaTaskProgress4Export(hqTaskId);
	         response.setContentType(
	                 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
	         response.setHeader("content-disposition", "attachment;filename="
	                 + URLEncoder.encode(getExcelName(hqTaskId), "UTF-8"));
	         workbook = ExcelUtils.createExcel(getExportData(areaTaskProgressInfo), getExportTitle());
	         outputStream = response.getOutputStream();
	         workbook.write(outputStream);
	         outputStream.flush();
	     } catch (Exception e) {
             LOGGER.error("主推任务完成情况导出失败", e);
         } finally {
             if (outputStream != null) {
                 outputStream.close();
             }
         }
	 }
	 
	 private List<String[]> getExportData(List<BiAreaTaskProgressVo> areaTaskProgressList){
	     List<String[]> exportList = new ArrayList<String[]>();
	     if(CollectionUtils.isNotEmpty(areaTaskProgressList)){
	         for(BiAreaTaskProgressVo item : areaTaskProgressList){
	             String[] value = {item.getDisCode(), item.getDisName(), String.valueOf(item.getRate().multiply(BigDecimal.valueOf(100))), 
	                     String.valueOf(item.getCateSaleAmount()), String.valueOf(item.getSaleAmountA()), String.valueOf(item.getSaleAmountB()),
	                     String.valueOf(item.getSaleAmountB2()), String.valueOf(item.getSaleRate()) + "%",
	                     String.valueOf(item.getTargetNumA()), String.valueOf(item.getTargetNumB()), String.valueOf(item.getSaleNumA()), 
	                     String.valueOf(item.getSaleNumB()), String.valueOf(item.getSaleNumB2()), 
	                     String.valueOf(item.getCompleteRate()) + "%"};
	             exportList.add(value);
	         }
	     }
	     return exportList;
	 }
	 
	 private String[] getExportTitle(){
         return new String[] { "大区编码", "大区名称", "目标占比", "品类销售总金额", "实际完成金额A类", "实际完成金额B类", "实际完成金额B2类", 
                 "销售占比", "任务销售台数A类", "任务销售台数B类", "实际完成台数A类", "实际完成台数B类", "实际完成台数B2类", "完成率",};
     }
	 
	 private String getExcelName(Long hqTaskId){
	     ReturnMsg<HqTask> hqTaskRet = mainPushTaskHqHandleService.queryTaskInfo(hqTaskId);
	     HqTask hqTask = hqTaskRet.getData();
	     String name = StringUtils.EMPTY;
	     if(null != hqTask){
	         name = hqTask.getMonthFormat().substring(5,7) + "月" + hqTask.getCateName() + "主推任务完成情况";
	     }
	     return name + ".xlsx";
	 }
	 
}
